#include <avr/io.h>
#include <util/delay.h>

void init_adc(void)
{

   /** Setup and enable ADC **/
   ADMUX = (0<<REFS1)|    // Reference Selection Bits
           (1<<REFS0)|    // AVcc - external cap at AREF
           (0<<ADLAR)|    // ADC Left Adjust Result
           (0<<MUX2)|     // Analog Channel Selection Bits
           (1<<MUX1)|     // ADC2 (PC2 PIN25)
           (0<<MUX0);
    
   ADCSRA = (1<<ADEN)|    // ADC ENable
           (0<<ADSC)|     // ADC Start Conversion
           (0<<ADATE)|    // ADC Auto Trigger Enable
           (0<<ADIF)|     // ADC Interrupt Flag
           (0<<ADIE)|     // ADC Interrupt Enable
           (1<<ADPS2)|    // ADC Prescaler Select Bits
           (0<<ADPS1)|
           (1<<ADPS0);
                   // Timer/Counter1 Interrupt Mask Register
    TIMSK1 |= (1<<TOIE1); // enable overflow interrupt
    
    
    TCCR1B |= (1<<CS11)|(1<<CS10);     // native clock
}

int read_adc(void)
{
   unsigned char i = 4;
   volatile unsigned int adc_temp=0;
   while (i--)
   {
      ADCSRA |= (1<<ADSC);
      while(ADCSRA & (1<<ADSC));
      adc_temp+= ADC;
      _delay_ms(50);
   }
   adc_temp = adc_temp / 4;  // Average a few samples
return adc_temp;
}

